package com.pawelthiel.util;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import com.framework.exceptions.PersistenceException;
import com.framework.log.Log;
import com.framework.log.LogLevel;
import com.framework.services.ServiceFacade;
import com.pawelthiel.persistence.dao.IDataAccessObject;
import com.pawelthiel.persistence.dao.implementation.UserDAO;
import com.pawelthiel.persistence.to.BenutzerrolleTO;
import com.pawelthiel.persistence.to.UserTO;

public class UserManager {

	private static final String TAG = "UserManager";
	
	private static Map<String, UserTO> users = new HashMap<String, UserTO>();
	
	public static void loadUser(String username, String password) {
		if (!UserManager.users.containsKey(username)) {
			try {
				getUserFromDatabase(username, password);
				
			} catch (SQLException e) {
				new Log(LogLevel.Error).write(LogLevel.Error.toString(), e.getMessage() + " in " + UserManager.TAG);
			} catch (ClassNotFoundException e) {
				new Log(LogLevel.Error).write(LogLevel.Error.toString(), e.getMessage() + " in " + UserManager.TAG);
			}
		}
	}

	private static void getUserFromDatabase(String username, String password) throws SQLException, ClassNotFoundException {
		System.out.println("Check if user is registered ...");
		UserDAO userDao = new UserDAO();
		UserTO user = userDao.findByFilter(username);
		if(user.getPassword() != null) {
			if(user.getPassword().equals(password)) {
				users.put(username, user);
				System.out.println("User registered.");				
			} 
		}
		else {
			System.out.println("Wrong password!");
		}
	}

	public static boolean isAuthorized(String username, String password) {
		boolean authorized = false;
		new Log(LogLevel.Info).write(null, "Checking user permissions for " + username);
		if(users.containsKey(username)) {
			UserTO user = users.get(username);
			if(!user.getPassword().equals(password)) {
				users.remove(username);
				new Log(LogLevel.Info).write(LogLevel.Info.toString(), "Incorrect password for " + username);
				return false;
			} else {
				authorized = true;
			}

		} else {
			new Log(LogLevel.Info).write(LogLevel.Info.toString(), "Incorrect password for " + username);
		}
		
		if(authorized) {
			new Log(LogLevel.Info).write(LogLevel.Info.toString(), "User " + username + " is authorized.");
		}
		return authorized;
	}

	public static UserTO getUser(String username) {
		UserTO user = new UserTO();
		if(users.containsKey(username)) {
			user = users.get(username);
		}
		return user;
	}
	
	public static boolean createNewUser(String vorname, String nachname, String password, BenutzerrolleTO rolle) {
		UserTO user = new UserTO();
		String id = vorname.substring(0,2) + nachname.substring(0,2);
		user.setId(id);
		user.setName(nachname);
		user.setVorname(vorname);
		user.setPassword(password);
		user.setRolle(rolle);
		
//		IDataAccessObject dao = ServiceFacade.getService(IDataAccessObject.class, user);
		IDataAccessObject<UserTO> dao = new UserDAO();
		
		try {
			dao.insert(user);
		} catch (PersistenceException e) {
			new Log(LogLevel.Error).write(LogLevel.Error.toString(), e.getMessage() + " in " + UserManager.TAG);
			return false;
		}
		
		return true;
	}

	public static void remove(String username) {
		if(users.containsKey(username)) {
			users.remove(username);
		}
	}
}
